﻿#include "base_line.h"

static bank1 int gBaseline[CHANNEL_NUM];        
bank3 signed long gFbaseLine[CHANNEL_NUM];  //baseline
bank3 unsigned long gRawStability[CHANNEL_NUM];
/*
* Description   : 获取最新baseline
* Paramter      : none
* Return        : gBaseline
*/
int Get_Base_Line(char ch_num)
{
	return gBaseline[ch_num];
}


bank1 int *temp_point;
/*
* Description   : baseline稳定性
* Paramter      : none
* Return        : 返回稳定值
*/
static unsigned int Check_Rawdata_Stability(char ch_num)
{
	long sum = 0;
	char i;
	int avg;
	int diff;
	
	if(ch_num == 1)  
	{
		#if CHANNEL_NUM == 2
			temp_point = gRawdataBackup_ch1;
		#endif 
	}else{
		temp_point = gRawdataBackup_ch0;
	}
	
	avg = Rawdata_Bankup_Avg(temp_point);
	
	// 计算乘方和
	for(i = (RAWDATA_BACKUP_NUM - RAWDATA_DIFF_NUM); i < RAWDATA_BACKUP_NUM; i++)
	{
		diff = temp_point[i] - avg;
		sum += diff*diff;
	}
	
	return (sum) / RAWDATA_DIFF_NUM;
}


/*
* Description   :Rawdata求平均
* Paramter      : none
* Return        : 返回计算的平均值
*/
static unsigned int Rawdata_Bankup_Avg(bank1 int *buf)
{
	long int sum = 0;
	char i;
	int avg;
	// 计算平均值
	for(i = (RAWDATA_BACKUP_NUM - RAWDATA_DIFF_NUM); i < RAWDATA_BACKUP_NUM; i++)
	{
		sum += buf[i];
	}
	avg = sum / RAWDATA_DIFF_NUM;
	
	return avg;
}
/*
* Description   : 获取前一段时间的最小值
* Paramter      : buf:需要寻找的数组  len：长度
* Return        : 最小值
*/
static int FindHistortMinBASE(bank1 int *buf,char len)
{
	char i;
	int min;
	
	min = buf[RAWDATA_BACKUP_NUM-1];
	for(i = RAWDATA_BACKUP_NUM-1; i>len;i--)
	{
		
		if(buf[i]<min)
		{
			min = buf[i];
		}
			
	}
	
	return min;
}
/*
* Description   : 获取前一段时间的最大值
* Paramter      : buf:需要寻找的数组  len：长度
* Return        : 最大值
*/
static int FindHistortMaxBASE(bank1 int *buf,char len)
{
	char i;
	int max;
	
	max = buf[RAWDATA_BACKUP_NUM-1];
	for(i = RAWDATA_BACKUP_NUM-1;i>len;i++)
	{
		if(buf[i]>max)
		{
			max = buf[i];
		}	
	}
	
	return max;
}
/*
* Description   : 基线复位
* Paramter      : data:需要复位位置
* Return        : none
*/
void BaseLineReset(int data,char ch_num)
{
	gBaseline[ch_num] = data;
	gFbaseLine[ch_num] = (((signed long)data)<<10);
}

/*
* Description   : 基线追踪
* Paramter      : none
* Return        : none
*/
void Baseline_Tracking(char ch_num)
{
	signed int delta_temp;
	int temp_sp,temp_limit;
	//高速追踪10帧
	if(
		//(gRel_Times<25)||离手多少帧内快速追基线 根据实际情况调整
			gPmuWorkMode != PMU_ACTIVE_MODE)
		{
			temp_sp = BASELINE_TRACK_SPEED_HIGH;
			temp_limit = BASELINE_CHANGE_LIMIT_HIGH;
		}else{
			temp_sp = BASELINE_TRACK_SPEED;
			temp_limit = BASELINE_CHANGE_LIMIT;
		}

	delta_temp = (Get_Rawdata(ch_num) - (gFbaseLine[ch_num] >> 10)) * temp_sp;
	if(delta_temp > temp_limit)
	{
		delta_temp = temp_limit;
	}
	if(delta_temp < -temp_limit)
	{
		delta_temp = -temp_limit;
	}
	gFbaseLine[ch_num] += delta_temp;
	
	gBaseline[ch_num] = (gFbaseLine[ch_num]>>10);

}
/*
* Description   : 基线处理
* Paramter      : none
* Return        : none
*/
void Base_Line_Process()
{
	char i;
//	
//	gRawStability[0] = 0;	
	//gRawStability[1] = 0;
	//gRawStability[0] = Check_Rawdata_Stability(0);
	for(i = 0 ; i< CHANNEL_NUM ;i++)
	{	
			//基线追踪
		if(
			!Get_Forceflag(i) 
			 && gRel_Times[i] > REL_TIME_TRI 
			//&& gRawStability[i] < DATA_STABILITY_TIR 
			)
		{
			Baseline_Tracking(i);	
		}
				
	}
}
	